package com.gzhryc.dictionary.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.dictionary.dao.CountryDao;
import com.gzhryc.jdbc.MultiDBTools;
import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.dictionary.dao.db.Country;
import com.gzhryc.dictionary.services.dto.CountrySearch;
import com.gzhryc.jdbc.Conditions;

public class CountryService {

	static Logger log = Logger.getLogger(CountryService.class);

	public static CountryService self() {
		return MultiDBTools.getService(CountryService.class, true);
	}

	public static CountryService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey,CountryService.class, true);
	}

	CountryDao dao;

	public CountryService(String jdbcKey) {
		this.dao = new CountryDao(jdbcKey);
	}
	
	public List<Country> findBySearch(CountrySearch search,Integer pageNo,Integer pageLimit){
		Conditions condition = dao.buildConditions(search);
		try {
			return dao.findByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Long countBySearch(CountrySearch search) {
		Conditions condition = dao.buildConditions(search);
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public Country getById(Long id) {
		try {
			return dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取中国
	 * @return
	 */
	public Country getChina() {
		Conditions condition = new Conditions(Country.class);
		condition.and().is("englishName", "China");
		try {
			return dao.get(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Country getOrAddChina() {
		Country country = getChina();
		if(country != null){
			return country;
		}else {
			country = new Country();
			country.setName("中国");
			country.setFullName("中华人民共和国");
			country.setEnglishName("China");
			country.setLang("zh");
			country.setCreateDate(new Date());
			try {
				if(dao.insert(country) > 0){
					return country;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}
	
	public Country getByEnglishName(String englishName) {
		if(StringUtils.isNotBlank(englishName)) {
			Conditions condition = new Conditions(Country.class);
			condition.and().is("englishName", englishName);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public Country getByName(String name) {
		if(StringUtils.isNotBlank(name)) {
			Conditions condition = new Conditions(Country.class);
			condition.and().is("name", name);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public boolean add(Country country) throws LogicException {
		Country temp = getByName(country.getName());
		if(temp != null) {
			throw new LogicException("已存在");
		}
		country.setCreateDate(new Date());
		try {
			return dao.insert(country) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateNotNull(Country country) throws LogicException{
		Country temp = getByName(country.getName());
		if(temp != null && !temp.getId().equals(country.getId())) {
			throw new LogicException("已存在");
		}
		try {
			return dao.updateNotNull(country) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteById(Long id) throws LogicException {
		try {
			return dao.deleteById(id) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
